<?php


/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program.  If not, see 
 * <http://www.gnu.org/licenses/>.
*/


// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();


jimport('joomla.application.component.model');


class TableImage extends JTable
{
	function __construct($db)
	{
		parent::__construct('#__gallery_image', 'id', $db);
	}

	function check()
	{
		if (JFilterInput::checkAttribute(array('href', $this->file_url)))
		{
			$this->setError(JText::_('Please provide a valid URL'));
			return false;
		}

		if (JFilterInput::checkAttribute(array('href', $this->thumb_url)))
		{
			$this->setError(JText::_('Please provide a valid URL'));
			return false;
		}

		// Check for a valid title.
		if ('' == trim($this->title)) 
		{
			$this->setError(JText::_('Your image must contain a title.'));
			return false;
		}

		if ('' == trim($this->file_url)) 
		{
			$this->setError(JText::_('You must specify an image.'));
			return false;
		}

		// Check for existing title.
		$query = 'SELECT id FROM #__gallery_images'
			. "\n	WHERE title = " . $this->_db->Quote($this->title)
			. "\n		AND catid = " . (int)$this->catid;
		$this->_db->setQuery($query);

		$xid = (int)$this->_db->loadResult();
		if ($xid && ((int)$this->id) != $xid) 
		{
			$this->setError(JText::sprintf('WARNNAMETRYAGAIN', JText::_('Image')));
			return false;
		}

		// Generate an alias from the title.
		$alias = JFilterOutput::stringURLSafe($this->title);
		if ('' != $this->alias)
			$this->alias = $alias;

		return true;
	}
}


?>
